package com.cdtye.itps.cms.service.equipment;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.cdtye.common.mybatiscore.base.BaseService;
import com.cdtye.itps.cms.dao.enums.EquipmentEnum;
import com.cdtye.itps.cms.dao.equipment.entity.EquipmentClassType;
import com.cdtye.itps.cms.dao.equipment.entity.EquipmentClassTypeChildren;
import com.cdtye.itps.cms.dao.equipment.vo.EquipmentClassTypeVo;
import com.cdtye.itps.cms.mapper.equipment.EquipmentClassTypeMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class EquipmentClassTypeService extends BaseService<EquipmentClassTypeMapper, EquipmentClassType> {


    @Autowired
    private EquipmentClassTypeMapper modelMapper;
    /**
     * 列表查询
     * @param vo
     * @return
     */
    public List<EquipmentClassTypeChildren> list(EquipmentClassTypeVo vo){
        /*LambdaQueryWrapper<EquipmentClassType> wrapper = new LambdaQueryWrapper<>();

        //添加查询条件
        List<EquipmentClassType> list = list(wrapper);*/
        List<Map<String, String>> list=modelMapper.getAll();
        ArrayList<EquipmentClassTypeChildren> children = new ArrayList<>();
        for (Map<String, String> e:list) {
            EquipmentClassTypeChildren equipmentClassTypeChildren = new EquipmentClassTypeChildren();
            equipmentClassTypeChildren.setId(e.get("id"));
            equipmentClassTypeChildren.setFid(e.get("fid"));
            equipmentClassTypeChildren.setTypeName(e.get("typeName"));
            equipmentClassTypeChildren.setTypeNo(e.get("typeNo"));
            children.add(equipmentClassTypeChildren);
        }
        List<EquipmentClassTypeChildren> bulid = this.bulid(children);

        return bulid;
    }

    /**
     * 分页查询，自定义sql语句
     * @param vo
     * @return
     */
    public IPage<Map<String,Object>> listPage(EquipmentClassTypeVo vo){
        return modelMapper.list(vo.page(), vo);
    }


    public IPage<Map<String, Object>> listByPage(EquipmentClassTypeVo vo) {

        if(vo.getId()!=null && vo.getId()!="" ) {
            EquipmentClassType equipmentClassType = modelMapper.selectById(vo.getId());
            vo.setTypeNo(equipmentClassType.getTypeNo());
        }
        String typeNo = vo.getTypeNo();
        if (typeNo != null && typeNo != "") {
            typeNo = typeNo + "%";
            vo.setTypeNo(typeNo);
        }
        String typeName = vo.getTypeName();
        if (typeName != null && typeName != "") {
            typeName = "%" + typeName + "%";
            vo.setTypeName(typeName);
        }
        IPage<Map<String, Object>> res=modelMapper.listByPage(vo.page(),vo);
        return  res;
    }


    public static List<EquipmentClassTypeChildren> bulid(List<EquipmentClassTypeChildren> treeNodes) {
        List<EquipmentClassTypeChildren> trees = new ArrayList<EquipmentClassTypeChildren>();
        for (EquipmentClassTypeChildren treeNode : treeNodes) {
            if ( "0".equals(treeNode.getFid())) {
                trees.add(treeNode);
            }
            for (EquipmentClassTypeChildren it : treeNodes) {
                if (treeNode.getId().equals(it.getFid())) {
                    if (treeNode.getChildren() == null) {
                        treeNode.setChildren( new ArrayList<EquipmentClassTypeChildren>());
                    }
                    treeNode.getChildren().add(it);
                }
            }
        }
        return trees;
    }

    /**
     * 判断下面是否有子集
     * @param byId
     * @return
     */
    public Integer isRedundant(String byId) {
        EquipmentClassTypeVo equipmentClassTypeVo = new EquipmentClassTypeVo();
        EquipmentClassType equipmentClassType = modelMapper.selectById(byId);
        String typeNo = equipmentClassType.getTypeNo();
        if (typeNo != null && typeNo != "") {
            typeNo = typeNo + "%";
            equipmentClassType.setTypeNo(typeNo);
        }
        equipmentClassTypeVo.setTypeNo(equipmentClassType.getTypeNo());
        List<Map<String,Object>> maps =modelMapper.isRedundant(equipmentClassTypeVo);

        return maps.size();
    }

    public List<Map<String,Object>> getCList() {
        List<Map<String,Object>> maps=modelMapper.getCList();
        return maps;
    }


    public List<Map<String,Object>> getByTypeNo(EquipmentClassTypeVo vo) {
        String typeNo = vo.getTypeNo();
        String substring = typeNo.substring(0, 3);
        vo.setTypeNo(substring);
        return modelMapper.getByTypeNo(vo);
    }


    public EquipmentClassType saveOrUpdates(EquipmentClassType equipmentClassType, EquipmentClassTypeVo vo) {
        EquipmentClassTypeVo vos = new EquipmentClassTypeVo();
        String typeNo="";
        //新增
        if(vo.getId()==null  || vo.getId().equals("")){
            //新增赋值
            if(StringUtils.isNotBlank(vo.getFid())){
                equipmentClassType.setFid(vo.getFid());
            }else{
                equipmentClassType.setFid("0");
            }
            equipmentClassType.setRemark(vo.getRemark());
            equipmentClassType.setTypeName(vo.getTypeName());
            equipmentClassType.setDelFlag(EquipmentEnum.YES.getText());
            equipmentClassType.setDtId(vo.getDtId());
            EquipmentClassType equipmentClassType1=null;
            Integer no=null;
            //获取父对象  fid id typeno获取当前子节点数量
            if((StringUtils.isNotBlank(vo.getFid()))){
                equipmentClassType1 = modelMapper.selectById(vo.getFid());
                vos.setTypeNo(equipmentClassType1.getTypeNo());
                no = this.selectMaxTypeNo(vos);
                typeNo = equipmentClassType1.getTypeNo();
                if(no<9){
                    int i = no + 1;
                    typeNo=typeNo+"00"+i;
                }else if(no>=9 && no<100){
                    int i = no + 1;
                    typeNo=typeNo+"0"+i;
                }else{
                    int i = no + 1;
                    typeNo=typeNo+"0"+i;
                }

            }else{
                vos.setTypeNo("");
                no = this.selectMaxTypeNo(vos);
                if(no<9){
                    int i = no + 1;
                    typeNo="00"+i;
                }else if(no>=9 && no<100){
                    int i = no + 1;
                    typeNo="0"+i;
                }else{
                    int i = no + 1;
                    typeNo="0"+i;
                }
                equipmentClassType.setFid("0");
            }
            equipmentClassType.setTypeNo(typeNo);

        }else {  //已有fname  remark  id  当前节点的typeno  typename  要 id  typename remark fid typeno
            equipmentClassType.setRemark(vo.getRemark());
            equipmentClassType.setTypeName(vo.getTypeName());
            equipmentClassType.setId(vo.getId());
            equipmentClassType.setDtId(vo.getDtId());

            Integer no=null;
            if((StringUtils.isNotBlank(vo.getFid()))){
                EquipmentClassType equipmentClassType1 = modelMapper.selectById(vo.getFid());
                vos.setTypeNo(equipmentClassType1.getTypeNo());
                no = this.selectMaxTypeNo(vos);
                typeNo = equipmentClassType1.getTypeNo();
                if(no<9){
                    int i = no + 1;
                    typeNo=typeNo+"00"+i;
                }else if(no>=9 && no<100){
                    int i = no + 1;
                    typeNo=typeNo+"0"+i;
                }else{
                    int i = no + 1;
                    typeNo=typeNo+"0"+i;
                }
                equipmentClassType.setFid(vo.getFid());
            }else{
                no = this.selectMaxTypeNo(vos);
                if(no<9){
                    int i = no + 1;
                    typeNo="00"+i;
                }else if(no>=9 && no<100){
                    int i = no + 1;
                    typeNo="0"+i;
                }else{
                    int i = no + 1;
                    typeNo="0"+i;
                }
                equipmentClassType.setFid("0");
            }
            equipmentClassType.setTypeNo(typeNo);

        }
        return equipmentClassType;
    }

    public Integer selectMaxTypeNo(EquipmentClassTypeVo vo) {
        String typeNo = vo.getTypeNo();
        if (typeNo == null && typeNo == "") {
            vo.setTypeNo("%");
        }else{
            typeNo = typeNo + "%";
            vo.setTypeNo(typeNo);
        }
        List<Map<String,Object>> types=modelMapper.selectMaxTypeNo(vo);
        String typeNo1 = (String) types.get(0).get("typeNo");
        String newStr = typeNo1.substring(typeNo1.length() - 3);
        Integer integer = Integer.valueOf(newStr);
        return integer;
    }


    public List<Map<String,Object>> selectListById(String id) {
        EquipmentClassType equipmentClassType = modelMapper.selectById(id);
        String typeNo = equipmentClassType.getTypeNo();
        if(typeNo!=null || typeNo!=""){
            typeNo=typeNo+"%";
        }else{
            typeNo="%";
        }
        EquipmentClassTypeVo equipmentClassTypeVo = new EquipmentClassTypeVo();
        equipmentClassTypeVo.setTypeNo(typeNo);
        List<Map<String, Object>> redundant = modelMapper.isRedundant(equipmentClassTypeVo);
        return redundant;
    }

    public Object getByIds(String id) {
        EquipmentClassTypeVo equipmentClassTypeVo = new EquipmentClassTypeVo();
        equipmentClassTypeVo.setId(id);
        Map<String,String> map=modelMapper.getByIds(equipmentClassTypeVo);
        return map;
    }

    public List<Map<String, String>> getAll() {
        List<Map<String, String>> all = modelMapper.getAll();
        return all;
    }
}
